<?php
/**
 * If you are not authorized to modify this file, do NOT touch it.
 * @file page.class.php
 * @author SkyWorld
 * @date 2010-12-2
 * @description Page object, using for show infomation in different pages
 */

  class page{
	/*
 	 *[ch]私有成员属性
 	 *[ch]保存数据表记录的所有记录的总条数
 	 *[ch]保存当前的页码
 	 *[ch]设置每页显示的条数
 	 *[ch]保存一共被分为多少页
 	 *[ch]保存从数据库中取记录的开始偏移数
 	 */
 	private $total;
 	private $page;
 	private $num ;
 	private $pageNum;
 	private $offset;

	/*
 	 * [ch]构造函数
 	 * [ch]$total为记录总条数
 	 * [ch]$page为当前页数
 	 * [ch]$num为每页显示的条数
 	 */
 	function __construct($total,$page=1,$num=5){
 		$this->total = $total;
 		$this->page	 = $page;
 		$this->num	 = $num;
 		$this->pageNum = $this->getPageNum();
 		$this->offset = $this->getOffset();
 	}

	/*
 	 * [ch]返回页面总数
 	 */
 	private function getPageNum(){
 		return ceil($this->total/$this->num);
 	}

	/*
	 * [ch]返回下一页的页面索引
	 */
 	private function getNextPage(){
 		if($this->page==$this->pageNum){
 			return false;
 		}else{
			return $this->page+1;
		}
 	}

	/*
	 * [ch]返回上一页的页面索引
	 */
 	private function getPrevPage(){
 		if($this->page==1){
 			return false;
 		}else{
			return $this->page-1;
 		}
 	}

	/*
	 * [ch]返回数据库查询所需要的偏移量
	 */
 	private function getOffset(){
 		return ($this->page-1)*$this->num;
 	}


	/*
	 *[ch]返回当前页开始的记录偏移数
	 */
 	private function getStartNum(){
 		if($this->total==0){
 			return 0;
 		}else{
 			return $this->offset+1;
 		}
 	}

	/*
	 * [ch]返回当前页结束的记录偏移数
	 */
 	private function getEndNum(){
 		return min($this->offset+$this->num , $this->total);
 	}

	/*
	 * [ch]该函数将所有和当前页面有关系的值放入一个数组一起返回
	 * [ch]row_total存放数据表中记录的总条数
	 * [ch]page_num存放每页显示的条数
	 * [ch]current_page存放当前页的索引
	 * [ch]row_offset当前页开始的偏移位置
	 * [ch]next_page下一页的索引
	 * [ch]prev_page上一页的索引
	 * [ch]page_start当前页开始的记录位置
	 * [ch]page_end当前页结束的记录位置
	 */
 	public function getPageInfo(){
 		$pageInfo = array(
 			'row_total' => $this->total,
 			'row_num'	=> $this->num,
 			'page_num'  => $this->getPageNum(),
 			'current_page' => $this->page,
 			'row_offset' => $this->getOffset(),
 			'next_page' => $this->getNextPage(),
 			'prev_page' => $this->getPrevPage(),
 			'page_start' => $this->getStartNum(),
 			'page_end' => $this->getEndNum()
 		);
 		return $pageInfo;
	}
 }
?>